package com.healthcloud.utils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * Created by xiam on 2015/6/10.
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class ApiRes<T> implements Serializable {

    private static final long serialVersionUID = 7596957801464577609L;

    public static final Integer SUCCESS_STATUS = 0; // 编码值：0-成功
    public static final Integer FAIL_STATUS = 1; // 编码值：1-失败
    public static final Integer INTERNAL_SERVER_ERROR_STATUS = 500; // 编码值：500-内部错误

    private boolean success;
    private T data;
    private String error;
    private String msg;
    private Integer code; // 编码值：0-成功；1-失败；500-内部错误

    @JsonIgnore
    public static final ApiRes SUCCESS = new ApiRes(true, SUCCESS_STATUS, null, null, SUCCESS_STATUS);

    public static final ApiRes ERROR = new ApiRes(false, FAIL_STATUS, null, null, FAIL_STATUS);

    public ApiRes(T data) {
        this(true, data, null, null, SUCCESS_STATUS);
    }

    public ApiRes(boolean result, T data, String error, String msg) {
        this(result, data, error, msg, -1);
    }

    /**
     * 成功返回
     */
    public static <T> ApiRes<T> success() {
        return ApiRes.<T>builder().code(SUCCESS_STATUS).data(null).success(true).msg("操作成功。").build();
    }

    /**
     * 成功返回
     */
    public static <T> ApiRes<T> success(String msg) {
        return ApiRes.<T>builder().code(SUCCESS_STATUS).data(null).msg(msg).success(true).build();
    }

    /**
     * 成功返回
     *
     * @param data
     */
    public static <T> ApiRes<T> success(T data) {
        return ApiRes.<T>builder().code(SUCCESS_STATUS).data(data).success(true).msg("操作成功。").build();
    }

    /**
     * 错误返回
     */
    public static <T> ApiRes<T> failure() {
        return failure(FAIL_STATUS, new Exception());
    }

    /**
     * 错误返回 异常
     */
    public static <T> ApiRes<T> failure(Exception exception) {
        return failure(FAIL_STATUS, exception);
    }

    /**
     * 错误返回 500-内部错误
     */
    public static <T> ApiRes<T> failure(String msg) {
        // return failure(INTERNAL_SERVER_ERROR_STATUS, new Exception(msg));
        return failure(FAIL_STATUS, new Exception(msg));
    }

    /**
     * 错误返回 code + 异常
     */
    public static <T> ApiRes<T> failure(Integer errorCode, Exception exception) {
        if (null == errorCode) {
            errorCode = FAIL_STATUS;
        }
        if (exception.getMessage().contains("操作失败")) {
            return ApiRes.<T>builder().code(errorCode).data(null).success(false).msg(exception.getMessage()).build();
        } else {
            return ApiRes.<T>builder().code(errorCode).data(null).success(false).msg("操作失败。" + exception.getMessage()).build();
        }
    }

    /**
     * 错误返回 code + msg
     */
    public static <T> ApiRes<T> failure(Integer errorCode, String msg) {
        return failure(errorCode, new Exception(msg));
    }

    public static <T> ApiRes<T> returnWrapperApiRes(ApiRes res) {
        if (ApiRes.SUCCESS_STATUS.equals(res.getCode())) {
            return ApiRes.<T>builder().code(SUCCESS_STATUS).data(null != res.getData() ? (T) res.getData() : null).msg(res.getMsg()).build();
        } else if (ApiRes.FAIL_STATUS.equals(res.getCode())) {
            return failure(res.getMsg());
        } else {
            return ApiRes.failure("系统异常，未知错误。");
        }
    }

}